package com.hongdee.scm.stock.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.scm.stock.service.ITScmStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 名  称:stockController
 * 类  型:class
 * 描  述:
 * 路  径:com.hongdee.scm.stock.controller
 * 作  者: 术洪超
 * 日  期:2017-05-12 13:59
 */

@RestController
@RequestMapping("/scm/stock")
public class TScmStockController {


    @Autowired
    private ITScmStockService itScmStockService;

    /**
     * @param data 要保存的实体
     * @return Map
     * @throws Exception
     * @desc 保存添加或者修改的实体内容，通过主键判断
     * @author 术洪超
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "type", defaultValue = "") String type,
                             @RequestBody Map data) throws Exception {
        this.itScmStockService.save(data, uid, type);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("入库成功！");
        return res;
    }


    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param start 起始记录
     * @param limit 页面大小
     * @return Map
     * @throws Exception
     * @desc 通过 JDBCDao分页查询
     * @author 术洪超
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestBody Map data) throws Exception {

        Map map = itScmStockService.page(start, limit, data, uid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * @param data 参数数组
     * @return
     * @throws Exception
     * @desc 录入盘点正常车辆类型
     * @author 术洪超
     */
    @RequestMapping("/updateRight")
    @ResponseBody
    public ResultEntity updateRight(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    @RequestBody Map data) throws Exception {
        ResultEntity res = new ResultEntity();
        itScmStockService.updateRight(data);
        res.setSuccess(true);
        res.setMsg("盘点结果录入成功！");
        return res;
    }

    /**
     * @param data 参数数组
     * @return
     * @throws Exception
     * @desc 录入盘亏车辆类型
     * @author 术洪超
     */
    @RequestMapping("/updateError")
    @ResponseBody
    public ResultEntity updateError(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    @RequestBody Map data) throws Exception {
        ResultEntity res = new ResultEntity();
        itScmStockService.updateError(data);
        res.setSuccess(true);
        res.setMsg("盘点结果录入成功！");
        return res;
    }


    /**
     * 已入库车辆
     *
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/newPage")
    @ResponseBody
    public Map newpage(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestParam(name = "start", defaultValue = "0") String start,
                       @RequestParam(name = "limit", defaultValue = "50") String limit,
                       @RequestParam(name = "status", defaultValue = "0") String status,
                       @RequestParam(name = "type", defaultValue = "") String type,
                       @RequestBody Map data) throws Exception {

        Map map = this.itScmStockService.newpage(data, status, start, limit, uid,type);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * @param data 参数数组
     * @return
     * @throws Exception
     * @desc 录入盘点结果
     * @author 术洪超
     */
    @RequestMapping("/saveDifferenceReason")
    @ResponseBody
    public ResultEntity saveDifferenceReason(@RequestParam(name = "uid", defaultValue = "") String uid,
                                             @RequestParam(name = "token", defaultValue = "") String token,
                                             @RequestBody Map data) throws Exception {
        ResultEntity res = new ResultEntity();
        itScmStockService.saveReason(data);
        res.setSuccess(true);
        res.setMsg("盘点差异因素录入成功！");
        return res;
    }

}
